<template>
  <div>
    <h2 class="page-title">
      <el-icon><Bell /></el-icon>
      报警管理
    </h2>
    <div class="card">
      <div class="table-operations">
        <el-input
          placeholder="搜索报警"
          style="width: 300px"
          v-model="alertSearch"
          clearable
        >
          <template #append>
            <el-button :icon="Search"></el-button>
          </template>
        </el-input>
      </div>

      <el-table
        :data="filteredAlertList"
        stripe
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column prop="title" label="报警内容"></el-table-column>
        <el-table-column prop="level" label="级别" width="100" align="center">
          <template #default="scope">
            <el-tag
              v-if="scope.row.level === 'high'"
              type="danger"
              effect="dark"
            >
              <el-icon><WarningFilled /></el-icon>
              紧急
            </el-tag>
            <el-tag v-else type="warning" effect="dark">
              <el-icon><Warning /></el-icon>
              警告
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pond"
          label="所属虾塘"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="device"
          label="相关装置"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="time"
          label="发生时间"
          width="140"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag
              v-if="scope.row.status === 'resolved'"
              type="success"
              effect="dark"
            >
              <el-icon><SuccessFilled /></el-icon>
              已处理
            </el-tag>
            <el-tag v-else type="danger" effect="dark">
              <el-icon><Clock /></el-icon>
              待处理
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              :icon="Edit"
              @click="handleProcess(scope.row)"
              v-if="scope.row.status === 'pending'"
            >
              处理
            </el-button>
            <el-button
              size="small"
              :icon="View"
              @click="handleView(scope.row)"
              v-else
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 报警详情对话框 -->
    <el-dialog :title="dialogTitle" v-model="detailVisible" width="600px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="报警ID">{{
          currentAlert.id
        }}</el-descriptions-item>
        <el-descriptions-item label="报警内容">{{
          currentAlert.title
        }}</el-descriptions-item>
        <el-descriptions-item label="级别">
          <el-tag v-if="currentAlert.level === 'high'" type="danger"
            >紧急</el-tag
          >
          <el-tag v-else type="warning">警告</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="currentAlert.status === 'resolved'" type="success"
            >已处理</el-tag
          >
          <el-tag v-else type="danger">待处理</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="所属虾塘">{{
          currentAlert.pond
        }}</el-descriptions-item>
        <el-descriptions-item label="相关装置">{{
          currentAlert.device
        }}</el-descriptions-item>
        <el-descriptions-item label="发生时间" :span="2">{{
          currentAlert.time
        }}</el-descriptions-item>
        <el-descriptions-item label="报警详情" :span="2">
          <div class="alert-detail">
            <p>这是一个详细的报警描述，包含具体的异常数据和可能的原因分析。</p>
            <p>建议处理措施：检查设备状态，调整相关参数。</p>
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailVisible = false">关闭</el-button>
          <el-button
            type="primary"
            @click="handleProcess(currentAlert)"
            v-if="currentAlert.status === 'pending'"
          >
            立即处理
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import {
  Bell,
  WarningFilled,
  Warning,
  SuccessFilled,
  Clock,
} from "@element-plus/icons-vue";

export default {
  name: "AlertList",
  components: { Warning, WarningFilled, Bell },
  setup() {
    const Search = ElementPlusIconsVue.Search;
    const View = ElementPlusIconsVue.View;
    const Edit = ElementPlusIconsVue.Edit;

    const alertSearch = ref("");
    const loading = ref(false);
    const detailVisible = ref(false);
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(0);

    const currentAlert = reactive({
      id: "",
      title: "",
      level: "",
      pond: "",
      device: "",
      time: "",
      status: "",
    });

    const alertList = ref([
      {
        id: 1,
        title: "1号虾塘溶氧量过低",
        level: "high",
        pond: "1号虾塘",
        device: "溶解氧传感器#1",
        time: "2023-06-15 14:25",
        status: "pending",
      },
      {
        id: 2,
        title: "3号虾塘pH值异常",
        level: "medium",
        pond: "3号虾塘",
        device: "pH传感器#3",
        time: "2023-06-15 13:40",
        status: "pending",
      },
      {
        id: 3,
        title: "5号虾塘水温过高",
        level: "medium",
        pond: "5号虾塘",
        device: "水温传感器#5",
        time: "2023-06-15 11:15",
        status: "pending",
      },
      {
        id: 4,
        title: "2号虾塘投饵机故障",
        level: "medium",
        pond: "2号虾塘",
        device: "投饵机#2",
        time: "2023-06-14 09:30",
        status: "resolved",
      },
      {
        id: 5,
        title: "4号虾塘水位过低",
        level: "high",
        pond: "4号虾塘",
        device: "水位传感器#4",
        time: "2023-06-13 16:45",
        status: "resolved",
      },
    ]);

    const filteredAlertList = computed(() => {
      if (!alertSearch.value) {
        return alertList.value;
      }
      return alertList.value.filter(
        (alert) =>
          alert.title.includes(alertSearch.value) ||
          alert.pond.includes(alertSearch.value) ||
          alert.device.includes(alertSearch.value)
      );
    });

    const dialogTitle = computed(() => {
      return `报警详情 - ${currentAlert.title}`;
    });

    const handleProcess = (row) => {
      ElMessageBox.confirm(`确定要处理报警 "${row.title}" 吗？`, "处理报警", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const index = alertList.value.findIndex(
            (alert) => alert.id === row.id
          );
          if (index !== -1) {
            alertList.value[index].status = "resolved";
            ElMessage.success("报警已处理");
          }
        })
        .catch(() => {});
    };

    const handleView = (row) => {
      Object.assign(currentAlert, row);
      detailVisible.value = true;
    };

    const handleSizeChange = (val) => {
      pageSize.value = val;
      currentPage.value = 1;
    };

    const handleCurrentChange = (val) => {
      currentPage.value = val;
    };

    onMounted(() => {
      total.value = alertList.value.length;
    });

    return {
      Bell,
      WarningFilled,
      Warning,
      SuccessFilled,
      Clock,
      Search,
      View,
      Edit,
      alertSearch,
      loading,
      alertList,
      filteredAlertList,
      currentPage,
      pageSize,
      total,
      detailVisible,
      currentAlert,
      dialogTitle,
      handleProcess,
      handleView,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped>
.page-title {
  font-size: 20px;
  margin-bottom: 20px;
  color: #1b9a7c;
  padding-bottom: 10px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-title .el-icon {
  font-size: 24px;
}

.table-operations {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.card {
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

:deep(.el-table .el-table__cell) {
  padding: 12px 0;
}

:deep(.el-tag) {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.alert-detail {
  padding: 10px;
  background-color: #f9f9f9;
  border-radius: 4px;
}

.alert-detail p {
  margin: 5px 0;
  line-height: 1.6;
}
</style>
